# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution	
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################


import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record

class wiz_my_purchase_orders_open(wizard.interface):
    def _open_my_purchase_orders(self, cr, uid, data, context):
        cr.execute("Select id from purchase_order where (user_purchase=%d and state<>'draft') or user_id=%d" % (uid,uid))
        purchase_id = cr.fetchall()
        
        value = {
            'domain': "[('id', 'in', "+str(purchase_id)+")]",
            'name': 'My Purchase Orders',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'purchase.order',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_my_purchase_orders, 'state':'end'}
        }
    }
wiz_my_purchase_orders_open('my.purchase.orders.open')

class wiz_my_purchase_project_open(wizard.interface):
    def _open_my_project_order(self, cr, uid, data, context):
        cr.execute('Select order_id from purchase_order_line,account_analytic_account a where account_analytic_id=a.id and user_id=%d' % uid )
        purchase_id = cr.fetchall()
        
        value = {
            'domain': "[('id', 'in', "+str(purchase_id)+")]",
            'name': 'My Project Orders',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'purchase.order',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_my_project_order, 'state':'end'}
        }
    }
wiz_my_purchase_project_open('my.project.orders.open')

class wiz_my_department_orders_open(wizard.interface):
    def _open_my_department_order(self, cr, uid, data, context):
        cr.execute("select id from purchase_order where (user_id in (select user_id from hr_department d,hr_department_user_rel where d.id=department_id and manager_id=%d)) or (user_purchase in (select user_id from hr_department d,hr_department_user_rel where d.id=department_id and manager_id=%d)) and state<>'draft'" % (uid,uid))
        purchase_id = cr.fetchall()

        value = {
            'domain': "[('id', 'in', "+str(purchase_id)+")]",
            'name': 'My Department Orders',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'purchase.order',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_my_department_order, 'state':'end'}
        }
    }
wiz_my_department_orders_open('my.department.orders.open')

class wiz_relative_department_orders_open(wizard.interface):
    def _open_relative_department_order(self, cr, uid, data, context):
        cr.execute('select id from purchase_order where user_purchase in (select user_id from hr_department d,hr_department_user_rel where d.id=department_id and manager_id=%d)' % uid )
        purchase_id = cr.fetchall()

        value = {
            'domain': "[('id', 'in', "+str(purchase_id)+")]",
            'name': 'Relative Department Orders',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'purchase.order',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_relative_department_order, 'state':'end'}
        }
    }
wiz_relative_department_orders_open('relative.department.orders.open')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

